//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibContrib.Resource.Configuration.Design
{
	/// <summary>
	/// Represents a Resource Manager defined in the application's configuration.
	/// </summary>
	[Image(typeof(ResourceManagerNode))]
	[SelectedImage(typeof(ResourceManagerNode))]
	public class ResourceManagerNode : ConfigurationNode
	{
		#region Fields
		private string resourceProviderName;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the <see cref="ResourceManagerData"/> object to store in the application's configuration file.
		/// </summary>
		/// <value>
		/// The <see cref="ResourceManagerData"/> object to store in the application's configuration file.
		/// </value>
		[Browsable(false)]
		public ResourceManagerData ResourceManagerData
		{
			get
			{
				return new ResourceManagerData(Name, resourceProviderName);
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceManagerNode"/> class.
		/// </summary>
		public ResourceManagerNode ()
			: this(new ResourceManagerData(InternalResources.DefaultResourceManagerName, InternalResources.AssemblyResourceProviderName))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceManagerNode"/> class with a <see cref="ResourceManagerData"/> object.
		/// </summary>
		/// <param name="resourceManagerData">The resource manager data.</param>
		public ResourceManagerNode (ResourceManagerData resourceManagerData)
			: base(( resourceManagerData == null ) ? InternalResources.DefaultResourceManagerNodeName : resourceManagerData.Name)
		{
			if ( resourceManagerData == null ) { throw new ArgumentNullException("resourceManagerData"); }
			this.resourceProviderName = resourceManagerData.ResourceProvider;
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Raises the <see cref="ConfigurationNode.ChildAdded"/> event and sets the name of the Resource Provider based on the child <see cref="ResourceProviderNode"/>.
		/// </summary>
		/// <param name="e">A <see cref="ConfigurationChangedEventArgs"/> that contains the event data.</param>
		protected override void OnChildAdded (ConfigurationNodeChangedEventArgs e)
		{
			base.OnChildAdded(e);
			ResourceProviderNode node = e.Node as ResourceProviderNode;

			resourceProviderName = node.Name;
		}
		#endregion
	}
}